package com.ruoyi.security.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.system.api.RemoteBmCodeService;
import com.ruoyi.system.api.domain.RemoteSysBmType;
import com.ruoyi.system.api.enums.SysBmCodeTypeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.utils.SecurityUtils;

import java.util.ArrayList;

import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.security.domain.AfHydrantDetail;
import com.ruoyi.security.mapper.AfFirHydrantMapper;
import com.ruoyi.security.domain.AfFirHydrant;
import com.ruoyi.security.service.IAfFirHydrantService;

/**
 * 消防栓台账Service业务层处理
 *
 * @author cjie
 * @date 2022-01-22
 */
@Service
public class AfFirHydrantServiceImpl implements IAfFirHydrantService {
    @Autowired
    private AfFirHydrantMapper afFirHydrantMapper;

    @Autowired
    private RemoteBmCodeService remoteBmCodeService;

    /**
     * 查询消防栓台账
     *
     * @param id 消防栓台账ID
     * @return 消防栓台账
     */
    @Override
    public AfFirHydrant selectAfFirHydrantById(String id) {
        return afFirHydrantMapper.selectAfFirHydrantById(id);
    }

    /**
     * 查询消防栓台账列表
     *
     * @param afFirHydrant 消防栓台账
     * @return 消防栓台账
     */
    @Override
    public List<AfFirHydrant> selectAfFirHydrantList(AfFirHydrant afFirHydrant) {
        return afFirHydrantMapper.selectAfFirHydrantList(afFirHydrant);
    }

    /**
     * 新增消防栓台账
     *
     * @param afFirHydrant 消防栓台账
     * @return 结果
     */
    @Transactional
    @Override
    public int insertAfFirHydrant(AfFirHydrant afFirHydrant) {
        //生成code
        RemoteSysBmType remoteSysBmType = remoteBmCodeService.genCode(afFirHydrant.getSysBmId(), SysBmCodeTypeValue.DEVICE);
        String nowCode = remoteSysBmType.getNowCode();//生成的唯一编码
        String deviceType = remoteSysBmType.getHierarchy();//层级
        afFirHydrant.setDevCode(nowCode);
        afFirHydrant.setDeviceType(deviceType);
        afFirHydrant.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
        afFirHydrant.setCreateTime(DateUtils.getNowDate());
        afFirHydrant.setCreateBy(SecurityUtils.getUsername());
        int rows = afFirHydrantMapper.insertAfFirHydrant(afFirHydrant);
        insertAfHydrantDetail(afFirHydrant);
        return rows;
    }

    /**
     * 修改消防栓台账
     *
     * @param afFirHydrant 消防栓台账
     * @return 结果
     */
    @Transactional
    @Override
    public int updateAfFirHydrant(AfFirHydrant afFirHydrant) {
        afFirHydrant.setUpdateTime(DateUtils.getNowDate());
        afFirHydrant.setUpdateBy(SecurityUtils.getUsername());
        afFirHydrantMapper.deleteAfHydrantDetailByHydrantId(afFirHydrant.getId());
        insertAfHydrantDetail(afFirHydrant);
        return afFirHydrantMapper.updateAfFirHydrant(afFirHydrant);
    }

    /**
     * 批量删除消防栓台账
     *
     * @param ids 需要删除的消防栓台账ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteAfFirHydrantByIds(String[] ids) {
        afFirHydrantMapper.deleteAfHydrantDetailByHydrantIds(ids);
        return afFirHydrantMapper.deleteAfFirHydrantByIds(ids);
    }

    /**
     * 删除消防栓台账信息
     *
     * @param id 消防栓台账ID
     * @return 结果
     */
    @Override
    public int deleteAfFirHydrantById(String id) {
        afFirHydrantMapper.deleteAfHydrantDetailByHydrantId(id);
        return afFirHydrantMapper.deleteAfFirHydrantById(id);
    }

    /**
     * 新增消防栓配套信息
     *
     * @param afFirHydrant 消防栓台账对象
     */
    public void insertAfHydrantDetail(AfFirHydrant afFirHydrant) {
        List<AfHydrantDetail> afHydrantDetailList = afFirHydrant.getAfHydrantDetailList();
        String id = afFirHydrant.getId();
        if (StringUtils.isNotNull(afHydrantDetailList)) {
            List<AfHydrantDetail> list = new ArrayList<AfHydrantDetail>();
            for (AfHydrantDetail afHydrantDetail : afHydrantDetailList) {
                afHydrantDetail.setHydrantId(id);
                afHydrantDetail.setId(IdUtil.getSnowflake(0, 0).nextIdStr());
                list.add(afHydrantDetail);
            }
            if (list.size() > 0) {
                afFirHydrantMapper.batchAfHydrantDetail(list);
            }
        }
    }
}
